package com.yoo.ons.function.me.like

import android.content.Context
import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.SimpleItemAnimator
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.yoo.module_base.utils.StatusBarUtil
import com.yoo.ons.base.BaseVMActivity
import com.yoo.ons.base.adpater.isRefreshing
import com.yoo.ons.databinding.ActivityMyLikeListBinding
import com.yoo.ons.dialog.ProfileInfoBannerDialogFragment
import com.yoo.ons.event.MessageEvent
import com.yoo.ons.function.home.FlashChatDialogFragment
import com.yoo.ons.model.WlmSubListModel
import org.greenrobot.eventbus.EventBus

/**
 * I Like列表页面
 */
const val MY_LIKE_LIST_ITEM_FLASH_CHAT = "MY_LIKE_LIST_ITEM_FLASH_CHAT"
const val MY_LIKE_LIST_ITEM_EMPTY = "MY_LIKE_LIST_ITEM_EMPTY"
const val MY_LIKE_LIST_ITEM_PROFILE_INFO = "MY_LIKE_LIST_ITEM_PROFILE_INFO"
const val MY_LIKE_LIST_ITEM_ERROR = "MY_LIKE_LIST_ITEM_ERROR"

class MyLikeListActivity : BaseVMActivity<ActivityMyLikeListBinding, MyLikeListViewModel>(
    ActivityMyLikeListBinding::inflate,
    MyLikeListViewModel::class.java
) {
    private val adapter by lazy { MyLikeListAdapter() }
    private var modelBean: WlmSubListModel? = null
    override fun initData() {
        initialViews()
        dispatchRequest()
        mViewModel?.dataAllList?.observe(this) {
            binding.titleBar.setTitleText("I Likes ${it.size-1}")
            adapter.submitList(it)
        }
        mViewModel?.loading?.observe(this) {
            binding.refreshLayout.isRefreshing = it.isRefreshing()
        }
        mViewModel?.likeTypeLiveData?.observe(this) {
            if (it == 3) {
                //信息弹窗里面点击了fc
                clickFlashChat(modelBean)
            }
        }
    }

    private fun dispatchRequest() {
        mViewModel?.reqIndexList(true)
    }

    private fun initialViews() {
        binding.titleBar.setBackListener { finish() }
        binding.refreshLayout.setOnRefreshListener {
            dispatchRequest()
        }
        binding.recyclerView.layoutManager =
            StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        (binding.recyclerView.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        binding.recyclerView.adapter = adapter
        adapter.setListener(::handleAdapterListener)
    }

    private fun handleAdapterListener(action: String, item: Any?) {
        when (action) {
            MY_LIKE_LIST_ITEM_FLASH_CHAT -> ((item as? View)?.tag as? WlmSubListModel)?.let(::clickFlashChat)
            MY_LIKE_LIST_ITEM_EMPTY -> (item as? View)?.let(::clickEmptyBtn)
            MY_LIKE_LIST_ITEM_PROFILE_INFO -> (item as? View)?.let(::showProfileInfo)
            MY_LIKE_LIST_ITEM_ERROR -> (item as? View)?.let(::tryAgainCLick)
        }
    }

    private fun tryAgainCLick(itemView: View?) {
        itemView?.let {
            mViewModel?.reqIndexList(true)
        }
    }

    private fun clickEmptyBtn(view: View?) {
        EventBus.getDefault().post(MessageEvent(2, ""))
        finish()
    }

    private fun showProfileInfo(view: View?) {
        view?.let {
            modelBean = it.tag as WlmSubListModel
            ProfileInfoBannerDialogFragment.newInstance(modelBean?.userCode ?: "", 2)
                .show(supportFragmentManager)
        }
    }

    private fun clickFlashChat(bean: WlmSubListModel?) {
        bean?.let {
            FlashChatDialogFragment.newInstance(bean.avatarUrl, bean.userCode, bean.nickName)
                .show(supportFragmentManager)
        }
    }

    override fun initView() {
        super.initView()
        binding.titleBar.setTitleBarPadding(StatusBarUtil.getStatusBarHeight(this))
    }

    override fun onDestroy() {
        binding.recyclerView.adapter = null
        adapter.setListener(null)
        binding.refreshLayout.setOnRefreshListener(null)
        super.onDestroy()
    }

    companion object {
        fun start(context: Context?) {
            val intent = Intent(context, MyLikeListActivity::class.java)
            context?.startActivity(intent)
        }
    }

}